<?php

namespace app\api\controller;

use app\api\model\Agreement;
use app\common\controller\Api;
use app\api\model\ListModel;
use think\Db;
use think\Request;
/**
 * 示例接口
 */
class NewsList extends Api{
    protected $where =['is_show'=>1];
    protected $noNeedLogin = ['agree'];
    protected $noNeedRight = ['*'];
    public function __construct(Request $request = null)
{
    $this->Model = new ListModel;
    parent::__construct($request);
}

    /**
     * @return data
     * 获取文章列表
     */
    public function info()
    {
        $input = input();
        if (empty($input)){
            $this->error("参数无效");
        }

        if($input['type'] == 'jing'){
            $order = 'weigh desc';
        }else{
            $order = 'showtime desc';
        }

        $where = [];
        $where['is_show'] = 1;
        if(isset($input['search']) && !empty($input['search'])){
            $where['l.title'] =['like','%'.$input['search'].'%'];
        }
        if(isset($input['category_id']) && !empty($input['category_id'])){
            $where['l.category_id'] = $input['category_id'];
        }
        $data =  $this->Model->alias('l')
            ->field("l.*")
            ->where($where)
            ->order($order)
            ->paginate($input['limit']);
        foreach ($data as $key => $val){
            $val['zanstatus'] = 0;
            $res =  Db::name('user_zan')->where(['user_id'=>$this->auth->id])->where(['list_id'=>$val['id']])->find();
            if($res){
                $val['zanstatus'] = 1;
            }
        }
        if(empty($data))
        {
            $this->error("无数据");
        }
        $this->success('请求成功',$data);
    }

    /**
     * @return data
     * 获取文章内容
     */
    public function content()
    {
        $input = input();
        if (empty($input['id'])){
            $this->error("参数无效");
        }
        $userid = $this->auth->id;
        $data = $this->Model
            ->where(['id'=>$input['id']])
            ->where($this->where)
            ->find();
        $res = Db::name('user_see')->where(['type'=>'see','list_id'=>$input['id'],'user_id'=>$userid])->find();
        $zan = Db::name('user_zan')->where(['type'=>'zan','list_id'=>$input['id'],'user_id'=>$userid])->find();
        if(!$res){
            $this->Model->where(['id'=>$input['id']])->setInc('see', 1);
            Db::name('user_see')->insert(['type'=>'see','user_id'=>$userid,'createtime'=>time(),'list_id'=>$input['id']]);
        }
        $data['zanstatus']= empty($zan) ? 0 : 1;
        if(empty($data))
        {
            $this->error("无数据");
        }
        $this->success('请求成功',$data,1);
    }
    /**
     * @return data
     * 点赞
     */
    public function fabulous()
    {
        $input = input();
        if (empty($input['id'])){
            $this->error("参数无效");
        }
        $userid = $this->auth->id;
        $where = ['type'=>'zan','list_id'=>$input['id'],'user_id'=>$userid];
        $res = Db::name('user_zan')->where($where)->find();
        if($res){
            $type = 0;
            $data = $this->Model->where(['id'=>$input['id']])->setDec('zan');
            Db::name('user_zan')->where($where)->delete();
        }else{
            $type = 1;
            $data = $this->Model->where(['id'=>$input['id']])->setInc('zan', 1);
            Db::name('user_zan')->insert(['type'=>'zan','user_id'=>$userid,'createtime'=>time(),'list_id'=>$input['id']]);
        }
        $row['type'] = $type;
        $row['id'] = $input['id'];
        $this->success('请求成功',$row);
    }

    /**
     * 用户协议
     * @throws \think\exception\DbException
     */
    public function agree()
    {
        $agree = Agreement::get(1);
        $this->success('',$agree);
    }
}
